package models

import (
	"golang.org/x/crypto/bcrypt"
	"time"
)

type Users struct {
	Id         int       `json:"id"`
	Uuid       string    `json:"uuid"`
	Username   string    `json:"username"`
	Password   string    `json:"password"`
	Headimgurl string    `json:"headimgurl"`
	CreatedAt  time.Time `json:"created_at"`
	UpdatedAt  time.Time `json:"updated_at"`
}

func (*Users) TableName() string {
	return "chat_users"
}

// HashPwd 加密用户密码
func (*Users) HashPwd(pwd string) (string, error) {
	hashPwd, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
	if err != nil {
		return "", err
	}

	return string(hashPwd), nil
}

// ComparePwd 比较用户密码
func (*Users) ComparePwd(hashPwd string, pwd string) error {
	return bcrypt.CompareHashAndPassword([]byte(hashPwd), []byte(pwd))
}
